package com.bob.clan.wxconnector.config;

import com.alibaba.fastjson.JSON;
import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Configuration;

import java.util.List;

/**
 * 定义扫码登录时webSocket的client与server之间通讯的消息标准
 * code：为本次通讯的业务编码
 * title：为本次通讯的业务定义
 * senderType,accepterType,扫码登录的三主：1为server,2为web-login页,3为h5-confirm页
 * msgData：本次通讯的消息体
 * 扫码登录的所有业务种类已定义在config里，初始化时已加载
 */
@Data
@Configuration
@ConfigurationProperties(prefix = "web-socket-msg")
public class WebSocketMsgConfig {
    private List<WsMsg> wsMsgList;



    @Data
    public static class WsMsg {
        private int code;
        private String title;
        private int senderType;
        private int accepterType;
        private String wsSessionId;
        private String msgData;
    }

    public WsMsg getByCode(int code){
        for (WsMsg wsMsg : wsMsgList) {
            if(wsMsg.getCode()==code){
                return wsMsg;
            }
        }
        throw new RuntimeException("对应的websocket通讯代码不存在，code:"+code);
    }

    public String msg2Json(WsMsg wsMsg) {
        return JSON.toJSONString(wsMsg);
    }

    public WsMsg json2Msg(String jsonMsg) {
        return JSON.parseObject(jsonMsg, WsMsg.class);
    }
}
